package logic

import (
	"context"
	"fmt"
	"zhihu/mobels/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProblemLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProblemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProblemLogic {
	return &AddProblemLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProblemLogic) AddProblem(in *zhihurpc.AddProblemRequest) (*zhihurpc.AddProblemResponse, error) {
	// todo: add your logic here and delete this line
	var provlem mysql.Problem
	provlem = mysql.Problem{
		Title:   in.Title,
		Content: in.Content,
		UserId:  in.UserId,
	}
	err := mysql.DB.Create(&provlem).Error
	if err != nil {
		return nil, fmt.Errorf("问题发布失败")
	}
	return &zhihurpc.AddProblemResponse{ProblemId: int64(provlem.ID)}, nil
}
